<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Pro extends Model
{   
    //产品配件AJAX
    public function searchItem($data,$lang){
        if($data['request'] == 'pro'){
            $pro2cate = getDatas('pro2cate',['*'],['procat_id'=>$data['cat_id']]);
            // return $pro2cate;
            if(!empty($pro2cate)){
                foreach ($pro2cate as $k => $v) {
                    if(isset($data['pro_id']) && $v['pro_id'] != $data['pro_id']){
                        $prolist[] = getSearch('pro','zu',['zu.id,mu.content'],['mu.lang'=>$lang,'zu.id'=>$v['pro_id']],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
                    }else{
                        $prolist[] = getSearch('pro','zu',['zu.id,mu.content'],['mu.lang'=>$lang,'zu.id'=>$v['pro_id']],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
                        // $prolist[] = getMultiLangName('pro',['*'],['id'=>$v['pro_id']],'id asc')[0];
                    }
                }
                return json_encode(array('code'=>2000,'msg'=>'OK','data'=>$prolist));
            }
        }elseif ($data['request'] == 'prochar') {
            $prochar = getDatas('prochar',['*'],['pro_id'=>$data['id']]);
            // return $prochar;
            if(!empty($prochar)){
                foreach ($prochar as $k => $v) {
                    $returnFeature[$k]['id'] =  $v['id'];
                    $returnFeature[$k]['content'] = lang('model').''.$v['model'].'/';
                    $prochar2feature = getDatas('profeature2char',['*'],['prochar_id'=>$v['id']]);
                    if(!empty($prochar2feature)){
                        foreach ($prochar2feature as $k1 => $v1) {
                            $feature = getSearch('proattr_value','zu',['zu.id,mu.content'],['mu.lang'=>$lang,'zu.id'=>$v1['profeature_value_id']],[['multi_lang mu','zu.mkey = mu.mkey']],'zu.id desc')[0];
                            $returnFeature[$k]['content'] .= $feature['content'].'/';
                        }
                    }
                }
            }else{
                $returnFeature=[];
            }
            return json_encode(array('code'=>2000,'msg'=>'OK','data'=>$returnFeature));

        }else if($data['request'] == 'searchText'){
            // return $this->data;
            if(isset($data['searchText']) && $data['searchText']){
                $join  = [['multi_lang mu','zu.mkey = mu.mkey']];
                $field = ['zu.*,mu.content'];
                $where['mu.lang'] = $lang;
                if(!isset($data['pro_id'])){
                    $data['pro_id'] = 0;
                }
                
                $where['zu.id'] = ['not in',$data['pro_id']];
                // 查找
                $where['zu.id|mu.content'] = ['like',"%".$data['searchText'].'%'];   
                $where['zu.is_item'] = 1;
                $productList = getSearch('pro','zu',$field,$where,$join,'zu.id desc');
                // dump($productList);die;
            }else{
                $productList = [];
            }

            return array('code'=>2000,'msg'=>'成功','data'=>$productList);
        }
    }

}